package com.kova.api.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	//时间格式化定义
	public static final String DATE_FMT="yyyy-MM-dd";
	public static final String DATE_HM_FMT="yyyy-MM-dd HH:mm";
	public static final String DATE_TIME_FMT="yyyy-MM-dd HH:mm:ss";
	public static final String DATE_NO_SPACE_FMT="yyyyMMdd";
	public static final String DATE_TIME_NO_SPACE_FMT="yyyyMMddHHmmss";
	
	//获取类型
	public static final String DATA_YEAR = "year";
	public static final String DATA_MONTH = "month";
	public static final String DATA_DAY = "day";
	
	/**
	 * 获取日期对应年，月，日值
	 * @param date
	 * @param type
	 * @return
	 */
	public static Integer getDateTypeValue(Date date,String type){

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (type.equals(DATA_YEAR)){
			return cal.get(Calendar.YEAR);
		}else if (type.equals(DATA_MONTH)){
			return cal.get(Calendar.MONTH) + 1;
		}
		else if (type.equals(DATA_DAY)){
			return cal.get(Calendar.DATE);
		}
		return null;
	}
	
	/**
	 * 得到当前日期/时间字符串
	 * 
	 * @return 返回日期/时间字符串
	 */
	public static String getNowDateTimeString(String type) {
		Date date = new Date();
		SimpleDateFormat formattxt = new SimpleDateFormat(type);
		return formattxt.format(date);
	}
	
	public static String getTimeStringByDate(Date d, String type) {
        SimpleDateFormat formattxt = new SimpleDateFormat(type);
        return formattxt.format(d);
    }

	/**
	 * 时间转utc时间字符串
	 * @param dataStr
	 * @param type
	 * @return
	 */
	public static String parseUtcStr(String dataStr,String type){
		//String str="2020-01-02 23:59:59";
		try {
			SimpleDateFormat sdf= new SimpleDateFormat(type);
			Date date =sdf.parse(dataStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
			int dstOffset = calendar.get(Calendar.DST_OFFSET);
			calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
			long timeInMillis = calendar.getTimeInMillis();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String rs=df.format(timeInMillis);
			return rs;
		}catch (Exception ex){
			ex.printStackTrace();
			return "";
		}
	}

	/**
	 * 日期转化为cron表达式
	 * @param date
	 * @return
	 */
	public static String getCronExpressionStr(Date  date){
		String dateFormat="ss mm HH dd MM ? yyyy";
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		String formatTimeStr = null;
		if (date != null) {
			formatTimeStr = sdf.format(date);
		}
		return formatTimeStr;
	}

	
	/**
	 * 日期转化为字符串  
	 * @param date   日期
	 * @param type   日期格式
	 * @return
	 */
	public static String getDateTimeString(Date date,String type) {
		SimpleDateFormat formattxt = new SimpleDateFormat(type);
		return formattxt.format(date);
	}
	
	/**
	 * 字符串转化为日期
	 * @param dateStr   时间字符串
	 * @param fmt       日期格式
	 * @return
	 */
	public static Date parse(String dateStr, String fmt) {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        try {
            if (dateStr != null) {
                return sdf.parse(dateStr);
            }
        } catch (ParseException e) {
        }
        return null;
    }

	
	public static int getTodatRemainingTime() {
        String nowStr = getNowDateTimeString(DATE_TIME_FMT);
        String endStr = nowStr.substring(0, 10) + " 23:59:59";
        return DateUtil.getTimeDelta(nowStr, endStr);
    }
	
	/**
	 * 两个时间相差秒数
	 * @param dateStr1
	 * @param dateStr2
	 * @return
	 */
	public static int getTimeDelta(String dateStr1, String dateStr2) {
		Date date1 = parse(dateStr1,DATE_TIME_FMT);
		Date date2 = parse(dateStr2,DATE_TIME_FMT);
        long timeDelta = (date1.getTime() - date2.getTime()) / 1000;// 单位是秒
        int secondsDelta = timeDelta > 0 ? (int) timeDelta : (int) Math.abs(timeDelta);
        return secondsDelta;
    }
	
	/**
	 * 日期加月
	 * @param date   时间
	 * @param index  添加几个月
	 * @return
	 */
	public static Date addMonth(Date date, int index) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, index);
		return cal.getTime();
	}
	
	/**
	 * 日期加天
	 * @param date   时间
	 * @param index  添加天数
	 * @return
	 */
	public static Date addDate(Date date, int index) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, index);
		return cal.getTime();
	}
	
	/**
	 * 日期加秒
	 * @param date   时间
	 * @param index  添加秒数
	 * @return
	 */
	public static Date addSecond(Date date, int index) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, index);
		return cal.getTime();
	}
	
	/**
	 * 日期加小时
	 * @param date   时间
	 * @param hour  添加小时
	 * @return
	 */
	public static Date addHour(Date date, int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		return cal.getTime();
	}
	
	/**
	 * 获取日期天数
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}
	
	 /* 
     * (时间戳为秒)将unix时间戳转化为正常显示时间
     */
	public static String timeStamp2Date(String timestampString,String type){  
	    Long timestamp = Long.parseLong(timestampString)*1000;  
	    String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
	    return date;  
	}
	
	 /* 
     * (时间戳为毫秒)将时间戳转换为时间
     */
    public static String stampToDate(String s,String type){
    	try {
    		String res;
	        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
	        long lt = new Long(s);
	        Date date = new Date(lt);
	        res = simpleDateFormat.format(date);
	        return res;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
       
    }
    
   /***
    * 每周的第一天
    * @param date 时间
    * @return
    */
    public static Date getFirstOfWeekDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        // 所在周开始日期
       // String data1 = new SimpleDateFormat(type).format(cal.getTime());
        //return data1;
        return cal.getTime();
    }
    
    /***
     * 每周的最后一天
     * @param date 时间
     * @return
     */
     public static Date getLastOfWeekDay(Date date) {
         Calendar cal = Calendar.getInstance();
         cal.setTime(date);
         int d = 0;
         if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
             d = -6;
         } else {
             d = 2 - cal.get(Calendar.DAY_OF_WEEK);
         }
         cal.add(Calendar.DAY_OF_WEEK, d+6);
         // 所在周结束日期
        // String data1 = new SimpleDateFormat(type).format(cal.getTime());
        // return data1;
         return cal.getTime();
     }
     
   /**
    * 获取当前日期月的最大天数
    * @param date
    * @return
    */
 	public static int getDaysOfMonth(Date date) {
 		Calendar calendar = Calendar.getInstance();
 		calendar.setTime(date);
 		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 	}
     
   /**
    * 获取当前日期月第一天
    * @param date
    * @return
    */
     public static Date getDateMonthFirstDay(Date date){
     	
     	//获取实例
     	Calendar calendarInstance = Calendar.getInstance();
     	//设置时间
     	calendarInstance.setTime(date);
     	
     	 //获取某月最小天数
         int firstDay = calendarInstance.getActualMinimum(Calendar.DAY_OF_MONTH);
         //设置日历中月份的最小天数
         calendarInstance.set(Calendar.DAY_OF_MONTH, firstDay);

         return calendarInstance.getTime();
     }
     
     /**
      * 获取当前日期月最后一天
      * @param date
      * @return
      */
     public static Date getDateMaxMonthDay(Date date){
     	
     	//获取实例
     	Calendar calendarInstance = Calendar.getInstance();
     	//设置时间
     	calendarInstance.setTime(date);
     	
     	//获取某月最大天数
         int lastDay = calendarInstance.getActualMaximum(Calendar.DAY_OF_MONTH);
         //设置日历中月份的最大天数
         calendarInstance.set(Calendar.DAY_OF_MONTH, lastDay);
         
         return calendarInstance.getTime();
     }
}
